﻿#ifndef GROUP_H
#define GROUP_H

#include <QWidget>
#include "tableView/tableviewheader.h"
#include <QStandardItemModel>
#include "server/server.h"
namespace Ui {
class Group;
}
typedef  struct USER_TABLE_DATA_T
{
    QString IP;//IP地址
    QString nNo;//用户名
    QString sName;//用户姓名
    QString nSex;//用户性别
    int nAge;//用户年龄
    int socketDescriptor;
    QString DeviceState;//手环链接状态
}USER_TABLE_DATA;

typedef QHash<QString, USER_TABLE_DATA> QHashUserTable;
typedef QHash<QString, USER_TABLE_DATA>::iterator QHashUserTableIterator;
class Group : public QWidget
{
    Q_OBJECT
    enum COLUMN_NUM_E
    {
        COLUMN_CHECKBOX = 0,
        COLUMN_NO,
        COLUMN_IP,
        COLUMN_ID,
        COLUMN_NAME,
        COLUMN_SEX,
        COLUMN_AGE,
        COLUMN_DEVICE

    };

public:
    explicit Group(QWidget *parent = nullptr);
    ~Group();
    void initTableView();
    void setColumnWidth();
    void resizeEvent(QResizeEvent *);
    void initServer();
    void RefreshTable();
    void paintEvent(QPaintEvent *event);

private:
    Ui::Group *ui;
    QStandardItemModel *m_pTableModel;
    TableViewHeader *m_pTableHeader;
    int m_nCheckedCount;//选中个数
    QHashUserTable m_userTableHash;
    Server *server;
    QList<int>socketDescriptors;
    int type;//0-团体，1-对抗

    void findDataByColumnName(QStandardItemModel *model, const QString &columnName, int rowIndex);
//    void findDataInThirdColumn(QStandardItemModel *model, const QString &searchValue);
    QList<QStandardItem *> findDataInThirdColumn(QStandardItemModel *model, const QString &searchValue);
private slots:
    void on_tableViewClick(QModelIndex nIndex);
    void on_tableViewDbClick(QModelIndex nIndex);
    void on_tableViewSort(int nColumn);
    void on_tableViewSelectAllCheckBoxClick(Qt::CheckState state);

    void receivedMsg(QString address, QString msg);

    void receivedMsg(QJsonObject & json);
    void addNewClient(QString);

    void removeClient(QString);


    void on_pushButton_exit_clicked();


    void on_pb_enter_clicked();

    void getType(int);

signals:
    void command_query(QJsonObject,int type);
    void command_toClient(int type,QJsonObject json);
    void SendUserTableInfo(QHashUserTable m_userTableHash);
};

#endif // GROUP_H
